package com.base.myproject.client.HR.attendance;

import java.util.ArrayList;
import java.util.List;

import com.base.myproject.client.busi.BaseStore;
import com.base.myproject.client.busi.BO.BusiObject;
import com.base.myproject.client.jdbc.DataSet;
import com.base.myproject.client.tools.Tools;
import com.base.myproject.client.ui.BaseGrid;
import com.base.myproject.client.ui.BaseJDBCPanel;
import com.base.myproject.client.ui.celleditor.BaseComboBoxX;
import com.extjs.gxt.ui.client.data.BaseModelData;
import com.extjs.gxt.ui.client.event.BaseEvent;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.Events;
import com.extjs.gxt.ui.client.event.Listener;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.form.DateField;
import com.extjs.gxt.ui.client.widget.form.DateTimePropertyEditor;
import com.extjs.gxt.ui.client.widget.form.TextField;
import com.extjs.gxt.ui.client.widget.grid.ColumnConfig;
import com.extjs.gxt.ui.client.widget.grid.ColumnModel;
import com.google.gwt.core.client.GWT;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.Label;
import com.google.gwt.user.client.ui.VerticalPanel;

/**
 * 补休/加班查询
 * 
 * @author cxghiu410@gmail.com
 * 
 */
public class SelectWorkAndRest extends BaseJDBCPanel {
	BaseComboBoxX dep = new BaseComboBoxX("name", "SystemInfo", "");
	DateField seach_by_start_date = new DateField();// 查询开始日期
	DateField seach_by_end_date = new DateField();// 查询结束日期
	Button seach_bt = new Button("查询");// 查询按钮
	TextField<String> name = new TextField<String>();// 员工姓名

	public SelectWorkAndRest() {
		init();
	}

	public void init() {
		initWeigetx();
	}
	
	public void complete() {
		super.complete();
	}

	public void initWeigetx() {
		seach_by_start_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_by_end_date.setPropertyEditor(new DateTimePropertyEditor(
				"yyyy-MM-dd"));
		seach_bt.addListener(Events.OnClick, getListener());

		HorizontalPanel form = new HorizontalPanel();
		form.add(new Label("请选择部门："));
		form.add(dep);

		form.add(new Label("  日期查询从："));
		form.add(seach_by_start_date);

		form.add(new Label("  到："));
		form.add(seach_by_end_date);

		form.add(new Label("  员工姓名："));
		form.add(name);
		form.add(seach_bt);
		// 垂直布局
		VerticalPanel vp = new VerticalPanel();
		vp.setSpacing(10);
		vp.add(form);
		vp.add(getBaseGrid());
		add(vp);
	}

	// 创建数据表
	BaseStore store = new BaseStore();
	// 创建一个grid
	BaseGrid<BusiObject> grid = null;

	BaseGrid<BusiObject> getBaseGrid() {
		if (grid == null) {
			createGrid();
		}
		return grid;
	}

	/**
	 * 为排班表定义31个text变量
	 */
	void createGrid() {
		List<ColumnConfig> columns = new ArrayList<ColumnConfig>();

		ColumnConfig C1 = new ColumnConfig("status", "审批状态", 65);
		columns.add(C1);

		ColumnConfig C2 = new ColumnConfig("bcode", "员工编号", 65);
		columns.add(C2);

		ColumnConfig C3 = new ColumnConfig("name", "申请人", 65);
		columns.add(C3);

		ColumnConfig C4 = new ColumnConfig("signDate", "填表日期", 65);
		columns.add(C4);

		ColumnConfig C6 = new ColumnConfig("useDate", "申请日期", 80);
		columns.add(C6);

		ColumnConfig C7 = new ColumnConfig("department", "所属部门", 65);
		columns.add(C7);

		ColumnConfig C9 = new ColumnConfig("type", "申请类型", 65);
		columns.add(C9);

		ColumnConfig C10 = new ColumnConfig("dateTime1", "时段一", 100);
		columns.add(C10);

		ColumnConfig C11 = new ColumnConfig("dateTime2", "时段二", 100);
		columns.add(C11);

		ColumnConfig C12 = new ColumnConfig("period", "时长", 65);
		columns.add(C12);

		ColumnModel cm = new ColumnModel(columns);

		grid = new BaseGrid<BusiObject>(store, cm, false, false);
		grid.setTitle("查询结果");
		Tools.setResize(grid, 0.99, 0.85);
	}

	public void to_seach() {
		String start_date = seach_by_start_date.getRawValue();
		String end_date = seach_by_end_date.getRawValue();
		String depName = dep.getRawValue();
		String bcodeName = name.getRawValue();

		String sql = "select * from hr_Attendance_ot where 1=1";

		if (!start_date.equals("")) {
			sql = sql + " and useDate between '" + start_date + "' and '"
					+ end_date + "'";
		}

		if (!depName.equals("")) {
			sql = sql + " and department='" + depName + "'";
		}

		if (!bcodeName.equals("")) {
			sql = sql + " and name='" + bcodeName + "'";
		}

		final com.base.myproject.client.tools.GreetingServiceAsync greetingService = GWT
				.create(com.base.myproject.client.tools.GreetingService.class);
		greetingService.getDataSet(sql, new AsyncCallback<DataSet>() {
			@Override
			public void onFailure(Throwable caught) {
			}

			@Override
			public void onSuccess(DataSet result) {
				store.removeAll();
				if (result.getRowCount() > 0) {// 返回结果为0
					for (int i = 0; i < result.getRowCount(); i++) {

						BaseModelData model = new BaseModelData();

						model.set("status", result.getValue(i, "status"));
						model.set("bcode", result.getValue(i, "bcode"));
						model.set("name", result.getValue(i, "name"));
						model.set("signDate", result.getValue(i, "signDate"));
						model.set("useDate", result.getValue(i, "useDate"));
						model.set("department", result
								.getValue(i, "department"));
						model.set("type", result.getValue(i, "type"));
						if (result.getValue(i, "Bgntime1") != null && result.getValue(i, "Endtime2") != null) {
							String time1 = result.getValue(i, "Bgntime1") + "至"
								+ result.getValue(i, "Endtime1");
							model.set("dateTime1", time1);
						} else {
							model.set("dateTime1", "");
						}
						if (result.getValue(i, "Bgntime2") != null && result.getValue(i, "Endtime2") != null) {
							String time2 = result.getValue(i, "Bgntime2") + "至"
									+ result.getValue(i, "Endtime2");
							model.set("dateTime2", time2);
						} else {
							model.set("dateTime2", "");
						}
						model.set("period", Float.valueOf(result.getValue(i, "period")));

						store.add(model, false, false);
					}

					grid.setStore(store);
					grid.getView().refresh(true);
				}
			}
		});
	}

	int sync = 0;
	Listener<BaseEvent> defaultlistener;

	Listener<BaseEvent> getListener() {
		if (defaultlistener != null) {
			return defaultlistener;
		}
		defaultlistener = new Listener<BaseEvent>() {
			public void handleEvent(BaseEvent be) {
				sync++;
				try {
					if (sync == 1) {
						if (be.getType() == Events.Select) {

						} else if (be.getType() == Events.OnClick) {
							if (be.getSource().equals(seach_bt)) {
								if (dep.getRawValue().equals("")
										&& (seach_by_start_date.getRawValue()
												.equals("") || seach_by_end_date
												.getRawValue().equals(""))
										&& name.getRawValue().equals("")) {
									MessageBox.alert("警告", "请选择查询条件,日期范围必须完整",
											null);
								} else {
									to_seach();
								}

							}
						}
					}
				} finally {
					sync--;
				}
			}

		};
		return defaultlistener;
	}
}
